package com.serkol.abmanager;


import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableAutoConfiguration
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    /**
     * 配置静态访问资源
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * SpringBoot自动配置本身并不会把/swagger-ui.html
         * 这个路径映射到对应的目录META-INF/resources/下面
         * 采用WebMvcConfigurerAdapter将swagger的静态文件进行发布;
         */
        registry.addResourceHandler("/favicon.ico").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/favicon.ico");

        //将所有/static/** 访问都映射到classpath:/static/ 目录下
/*
        registry.addResourceHandler("/static/admin/*").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/static/admin/index.html");

        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/static/");
        registry.addResourceHandler("/static/image/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/static/image");
        registry.addResourceHandler("/static/apps/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/apps");
        registry.addResourceHandler("/.well-known/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/.well-known/");


      //  super.addResourceHandlers(registry);*/
    }

        @Override
    public void addCorsMappings(CorsRegistry registry) {
    /*    registry.addMapping("/**")    // 允许跨域访问的路径
                .allowedOrigins("*")    // 允许跨域访问的源
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")    // 允许请求方法
                .maxAge(168000)    // 预检间隔时间
                .allowedHeaders("*")  // 允许头部设置
                .allowCredentials(true);    // 是否发送cookie*/

        //添加映射路径
       /* //将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addMapping("/static/**",ResourceUtils.CLASSPATH_URL_PREFIX +"/static/");
        registry.addResourceHandler("/.well-known/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/.well-known/");*/
        registry.addMapping("/**")
                //放行哪些原始域
                .allowedOrigins("*")
                //是否发送Cookie信息
                .allowCredentials(true)
                //放行哪些原始域(请求方式)
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                //放行哪些原始域(头部信息)
                .allowedHeaders("*")
                //暴露哪些头部信息（因为跨域访问默认不能获取全部头部信息）
                .exposedHeaders("token");
    }
    @Bean
    @Primary
    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter(){
        MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper=new ObjectMapper();
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        converter.setObjectMapper(mapper);
        return converter;
    }
}
